<?php
namespace App\Wen\Utils;
use App\Models\Shop\WxShop;
use App\Models\Shop\WxShopClassify;
use App\Models\User\WxUser;
use App\Models\WxIcon;
use Dcat\Admin\Admin;
use Illuminate\Support\Facades\Cache;

class AdminUtils{


    public static function key_value_process($list){
        $keys = _array_key($list, 'keys', []);
        $values = _array_key($list, 'values', []);
        if(!_empty_($keys)){
            $res = [];
            for ($i = 0; $i < count($keys); $i++){
                $res[$keys[$i]] = $values[$i];
            }
            return json_encode($res);
        }
        return '[]';
    }

    public static function list_fiele_process($list){
        $list = _array_key($list, 'values', []);
        if($list){
            $res = [];
            foreach ($list as  $key=>$label ){
                if($key == '_def_'){
                    continue;
                }
                if(!_empty_($label)){
                    $res[] = $label;
                }
            }
            return json_encode($res);
        }
        return '[]';
    }

    public static function _sqlupdate_a2b(){

    }

    public static function external_link($url){
        $list = Settings::get('doc_url_replace_list', []);
        if(isset($list[$url])){
            return $list[$url];
        }
        return $url;
    }

    public static function _multipleSelectTable_order_process($cache_key, $data){
        if(_empty_($data)){
            return '';
        }
        if(!is_string($data)){
            return $data;
        }
        $select_arr = explode(',', $data);
        $last_select_arr = [];
        if($cache_key){
            $cache_values = json_decode(Cache::get($cache_key, '[]'), true);
            if($cache_values){
                foreach ($cache_values as $cache_value){
                    if(in_array($cache_value, $select_arr)){
                        $last_select_arr[] = $cache_value;
                    }
                }
                foreach ($select_arr as $select){
                    if(!in_array($select, $last_select_arr)){
                        $last_select_arr[] = $select;
                    }
                }
            }else{
                $last_select_arr = $select_arr;
            }
        }else{
            $last_select_arr = $select_arr;
        }
        return implode(',', $last_select_arr);
    }

    public static function _table_data($data, $return_arr = 0){
        if(_empty_($data)){
            return '[]';
        }
        $v_ = [];
        if(is_array($data)){
            foreach ($data as $new_){
                if($new_ === null){
                    continue;
                }
                if(_array_key($new_, '_remove_', null) == 1){
                    continue;
                }
                if(isset($new_['_remove_'])){
                    unset($new_['_remove_']);
                }
                if(is_string($new_)){
                    $new_ = trim($new_);
                }else if(is_array($new_)){
                    if(end($new_) === null){
                        array_pop($new_);
                    }
                    if(is_array($new_)){
                        foreach ($new_ as $i => $new__){
                            if(is_array($new__)){
                                if(end($new__) === null){
                                    array_pop($new_[$i]);
                                }
                            }
                        }
                    }
                }
                $v_[] = $new_;
            }
            if($return_arr){
                return $v_;
            }else{
                return json_encode($v_);
            }
        }
        return '[]';
    }


    public static function get_nav_left_right_icon($key, $multi = false){

        $value = Settings::get($key, '', $multi);
        if(_empty_($value)){
            return ['left'=>[], 'right'=>[]];
        }

        $icon_arr = explode(',', $value );
        if($icon_arr){
            return [
                'left' => _array_key($icon_arr, 0, '') ? WxIcon::find(_array_key($icon_arr, 0, '')) : null,
                'right' => _array_key($icon_arr, 1, '') ? WxIcon::find(_array_key($icon_arr, 1, '')) : null,
            ];
        }
    }

    public static function get_admin_shop(){
        $admin_user = Admin::user();
        if(_empty_($admin_user->uid)){
            return null;
        }
        $shop_id = WxUser::where('id', $admin_user->uid)->value('shop_id');
        if(_empty_($shop_id)){
            return null;
        }
        return WxShop::where('id', $shop_id)->first(FieldUtils::shopInfoColums());
    }

    public static function get_admin_user_shop_classifys(){
        global $__MINI_GLOBAL_IS_ADMIN_SUPER__;
        if($__MINI_GLOBAL_IS_ADMIN_SUPER__){
            return WxShopClassify::where('depth', 3)->pluck('name','id')->toArray();
        }
        $admin_user = Admin::user();
        if($admin_user->isAdministrator() || $admin_user->isRole('enterprise_admin')){
            return WxShopClassify::where('depth', 3)->pluck('name','id')->toArray();
        }
        if(_empty_($admin_user->uid)){
            return [];
        }
        $shop_id = WxUser::where('id', $admin_user->uid)->value('shop_id');
        if(_empty_($shop_id)){
            return [];
        }
        $shop_classifys = WxShop::where('id', $shop_id)->value('shop_classifys');
        if(is_string($shop_classifys)){
            $shop_classifys = explode(',', $shop_classifys);
        }else if(is_array($shop_classifys)){

        }
        return WxShopClassify::whereIn('id', $shop_classifys)->where('depth', 3)->pluck('name','id')->toArray();
    }

}
